// 小程序主代码，可以调用IDP相关接口

IDP.Miniapp.view.defaultFrame.mount(IDP.Miniapp.view.mountPoints.leftPanel);
IDP.Events.on('IDP.Interaction.SelectedElementsChange', (elementIds) => {
    const [elementId] = elementIds
    if (!elementId) return
    // console.table({ elementId });
    IDP.Miniapp.view.defaultFrame.postMessage({ action: 'onSelectedElementsChange', value: elementId.id })
})
IDP.Miniapp.view.defaultFrame.onMessageReceive(async data => {
    // console.log(data);

    if (data.action === 'getNeededModelData') {
        const EToolType = IDP.Custom.Common.ToolType
        const topModels = await IDP.Custom.Design.CustomModel.findTopModelsAsync({
            toolType: [
                EToolType.Cabinet,
                EToolType.Wardrobe,
                EToolType.DoorWindow,
                EToolType.CabinetCopy,
                EToolType.WardrobeCopy,
                EToolType.DoorWindowCopy,
            ]
        })
        const neededModelData = topModels.map(model => {
            const id = model.id
            const name = model.getName()
            const position = model.getPosition()
            const size = model.size
            return { id, name, position, size }
        })
        // console.log({ neededModelData, topModels });
        IDP.Miniapp.view.defaultFrame.postMessage({ action: 'neededModelData', value: neededModelData })
    } else if (data.action === 'highlightModel') {
        // console.log(data.value);
        const model = await IDP.Custom.Design.CustomModel.getCustomModelByModelIdAsync(data.value)
        const elementId = model?.getElementId()
        if (elementId) {
            IDP.Interaction.setSelectedElements([elementId as any])
        }
        IDP.Custom.Design.CustomModel.highlightModels({ modelIds: [data.value] })
    }
});
